#include <stdio.h>
#include "my_app_nvs.h"



void nvs_wifi_save_check(int updateCode, char* ssdi_in, char* password_in){
    /* 定义一个NVS操作句柄 */
    nvs_handle wificfg_nvs_handler;
    /* 打开一个NVS命名空间 */
    ESP_ERROR_CHECK( nvs_open("WiFi_cfg", NVS_READWRITE, &wificfg_nvs_handler) );
    uint32_t wifi_update = 0;
    esp_err_t ret = nvs_get_u32(wificfg_nvs_handler,"wifi_update",&wifi_update);
    if(updateCode == wifi_update )
        printf("wifi_cfg needn't to update. \n");
    else
    {
        printf("wifi_cfg update now... \n");
        ESP_ERROR_CHECK( nvs_set_str(wificfg_nvs_handler,"wifi_ssid",ssdi_in));
        ESP_ERROR_CHECK( nvs_set_str(wificfg_nvs_handler,"wifi_passwd",password_in)); 
        ESP_ERROR_CHECK( nvs_set_u32(wificfg_nvs_handler,"wifi_update",updateCode));
        printf("wifi_cfg update ok. \n");
    }
    ESP_ERROR_CHECK( nvs_commit(wificfg_nvs_handler) ); /* 提交 */
    nvs_close(wificfg_nvs_handler);                     /* 关闭 */
}


void save_wifi_information(char* ssdi_in, char* password_in){
    nvs_handle wificfg_nvs_handler;
    ESP_ERROR_CHECK( nvs_open("WiFi_cfg", NVS_READWRITE, &wificfg_nvs_handler) );
    ESP_ERROR_CHECK( nvs_set_str(wificfg_nvs_handler,"wifi_ssid",ssdi_in) );
    ESP_ERROR_CHECK( nvs_set_str(wificfg_nvs_handler,"wifi_passwd",password_in) );
    ESP_ERROR_CHECK( nvs_commit(wificfg_nvs_handler) ); /* 提交 */
    nvs_close(wificfg_nvs_handler);                     /* 关闭 */ 
    printf("smartconfig save wifi_cfg to NVS .\n");
}